package de.ifi.lmu.snitzelhunt.creator;

import java.util.ArrayList;
import java.util.Date;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import de.ifi.lmu.snitzelhunt.basics.*;

/**
 * 
 * @author Carl Oechsner
 * 
 * @Description Objekt dieser Klasse wird erstellt, sobald der User die
 *              Creator-App aufruft Dadurch kann in jedem Schritt Information
 *              aufgenommen werden und am Schluss eine fertige ArrayList mit
 *              Tasks an die Datenbank �bergeben werden.
 * 
 *              funktioniert aktuell nur f�r QuizTasks
 * 
 */
public class GatherRouteData {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6179151002733048783L;
	private Route tempRoute;
	private Task tempTask;
	private ArrayList<Task> tempTaskList;
	private int mData;

	/**
	 * function to set the route title
	 * 
	 * @param title
	 */
	public void setRouteName(String title) {
		if (tempRoute == null) {
			System.out.println("set new route with name :"
					+ tempRoute.getName());
			tempRoute = new Route(getNewLocalId(), getNewGlobalID(),
					getMyNumber(), null, title, null, null, null);
		} else {
			tempRoute.setName(title);
		}
	}

	private String getMyNumber() {
		// TODO Auto-generated method stub
		return null;
	}

	private int getNewLocalId() {
		// TODO Auto-generated method stub
		return 0;
	}

	private int getNewGlobalID() {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * function to set participants of a route
	 * 
	 * @param numbers
	 */
	public void setParticipants(ArrayList<User> numbers) {
		if (tempRoute == null) {
			tempRoute = new Route(getNewLocalId(), getNewGlobalID(),
					getMyNumber(), numbers, "", null, null, null);
		} else {
			tempRoute.setUsers(numbers);
		}
	}

	public void setDueDate(Date due) {
		if (tempRoute == null) {
			tempRoute = new Route(getNewLocalId(), getNewGlobalID(),
					getMyNumber(), null, "", null, null, due);
		} else {
			tempRoute.setEndDate(due);
		}
	}

	/**
	 * For QuizTasks: ******************
	 * 
	 * Questions and answers are saved to the QuizTask
	 * 
	 * 
	 * @param quest
	 *            question phrase as defined by user
	 * @param rightAns
	 *            right answer adbu
	 * @param wAns1
	 *            wrong answer adbu
	 * @param wAns2
	 *            wrong answer adbu
	 * @param wAns3
	 *            wrong answer adbu
	 */
	public void addQuizQandA(String quest, String rightAns, String wAns1,
			String wAns2, String wAns3) {
		// put answers in an array
		String tempAns[] = new String[4];

		tempAns[0] = rightAns;
		tempAns[1] = wAns1;
		tempAns[2] = wAns2;
		tempAns[3] = wAns3;

		// if tempTask is already instantiated, just set question and answers
		if (tempTask != null) {
			((QuizTask) tempTask).setQuestion(quest);
			((QuizTask) tempTask).setAnswers(tempAns);
			Log.i("gatherQuestions", "used existing quiztask");
			// if not already instantiated, create a new QuizTask with
			// coordinates set to 0
		} else {
			tempTask = new QuizTask(0, 0, quest, tempAns);
			Log.i("GatherQuestions", "created new Quiztask");
		}
	}

	/**
	 * For QuizTasks: ******************
	 * 
	 * 
	 * Latitude and longitude are added to the QuizTask object
	 * 
	 * if tempTask is already instantiated, just set coordinates if not, create
	 * a new QuizTask object
	 * 
	 * @param lat
	 *            latitude as defined by user
	 * @param lon
	 *            longitude as defined by user
	 */
	public void addQuizCoordinates(double lat, double lon) {
		//
		if (tempTask == null) {
			tempTask = new QuizTask(lat, lon, "", null);
			Log.i("gaterLoc", "created new Quiztask");
		} else {
			tempTask.setLatitude(lat);
			tempTask.setLongitude(lon);
			Log.i("gatherloc", "used existing quiztask");
		}
	}

	public void addActionTask(String description, double lat, double lon) {
		if (tempTask == null) {
			tempTask = new ActionTask(lat, lon, description);
			Log.i("newActionTask", "new actiontask creATED");
		} else {
			Log.i("gather", "tampTask already exists");
			((ActionTask) tempTask).setAction(description);
			tempTask.setLatitude(lat);
			tempTask.setLongitude(lon);
		}

	}

	/**
	 * check if all variables of a task have been set before adding them to the
	 * route
	 * 
	 * @return if there is somethin missing in the Task data, the method returns
	 *         false, otherwise true
	 * 
	 * 
	 */
	public boolean addToRoute() {
		if (tempTask == null) {
			Log.e("gatherData", "No task to add");
			return false;

			// if the current tempTask is a QuizTask, check contents
		} else {
			Log.i("gather", "trying to add task");
			addCurrentTask(tempTask);
			Log.i("gatherData", "ActionTask added");
			return true;
		}
	}

	/**
	 * call this when a task is complete and the user clicks on save at the end
	 * of the task procedure
	 * 
	 * @param tempTask2
	 *            the finished Task
	 */
	private void addCurrentTask(Task tempTask2) {
		// if it is the first Task, create a new ArrayList for Tasks
		if (tempTaskList == null) {
			tempTaskList = new ArrayList<Task>();
		}
		tempTaskList.add(tempTask2);
		Log.i("gather", "currentTask:" + tempTask2.getDetail());

		resetTask();
	}

	/**
	 * call if user exits the "Add task" menu
	 */
	public void resetTask() {
		tempTask = null;
	}

	public boolean submitRoute() {
		if (tempRoute == null) {
			Log.e("gatherData", "No route created");
			return false;
		} else {
			if (tempRoute.getCreator() == "") {
				Log.e("gatherData", "creator missing");
				return false;
			} else if (tempRoute.getEndDate() == null) {
				Log.e("gatherData", "end date missing");
				return false;
			} else if (tempRoute.getName() == "") {
				Log.e("gatherData", "the route has no name");
				return false;
			} else if (tempRoute.getTasks() == null
					|| tempRoute.nrOfTasks() == 0) {
				Log.e("gatherData", "route has no tasks");
				return false;
			} else if (tempRoute.getUsers() == null
					|| tempRoute.nrOfParticipants() == 0) {
				Log.e("gatherData", "route has no participants");
				return false;
			} else {
				// TODO: submit to database
				resetRoute();
				return true;

			}
		}
	}

	private void resetRoute() {
		tempRoute = null;
	}

	public ArrayList<Task> retrieveTaskList() {
		return tempTaskList;
	}

	public void removeTask(int selectedTask) {
		tempTaskList.remove(selectedTask);
		Log.i("gather", "removed task" + selectedTask);
	}

	public static final Parcelable.Creator<GatherRouteData> CREATOR = new Parcelable.Creator<GatherRouteData>() {

		@Override
		public GatherRouteData[] newArray(int size) {
			return new GatherRouteData[size];
		}

		@Override
		public GatherRouteData createFromParcel(Parcel source) {
			return new GatherRouteData(source);
		}
	};

	public GatherRouteData() {
	}

	public GatherRouteData(Parcel p) {

	}

}
